package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.comment.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowCommentConfigRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowRuTimeoutSettingRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.FlowCommonCodeConstants;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程意见配置
 *
 * @author liuhuimin
 * @date 2021-11-02
 */
@Api(value = "流程意见配置", tags = "流程意见配置")
@RestController
@RequestMapping("/comment/config")
public class FlowCommentConfigController {

    @Autowired
    private FlowCommentConfigRepository flowCommentConfigRepository;

    @Autowired
    private FlowRuTimeoutSettingRepository flowRuTimeoutSettingRepository;

    /**
     * 查询通知模板列表
     *
     * @param flowCommentConfigQueryVo
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FlowCommentConfigQueryVo flowCommentConfigQueryVo) {
        ResponseResult responseResult = flowCommentConfigRepository.list(flowCommentConfigQueryVo);
        return responseResult;
    }

    /**
     * 新增模板
     *
     * @param flowCommentConfigAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated FlowCommentConfigAddVo flowCommentConfigAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
//        if (flowCommentConfigAddVo.getStatus()){
//            String type = flowCommentConfigAddVo.getType();
//            if (flowCommentConfigRepository.isTemplateExist(type)) {
//                return ResponseResult.fail(ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errCode(), ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errMessage());
//            }
//        }
        return ResponseResult.success(flowCommentConfigRepository.insert(flowCommentConfigAddVo));
    }

    /**
     * 更新模板
     */
    @PostMapping("/edit")
    public ResponseResult update(@RequestBody @Validated FlowCommentConfigUpdateVo flowCommentConfigUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
//        if (flowCommentConfigUpdateVo.getStatus()){
//            String type = flowCommentConfigUpdateVo.getType();
//            Long id = flowCommentConfigUpdateVo.getId();
//            if (flowCommentConfigRepository.isTemplateExist(type,id)) {
//                return ResponseResult.fail(ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errCode(), ErrorCode.NOTICE_TYPE_TEMPLATE_EXIST.errMessage());
//            }
//        }
        Integer updateRow = flowCommentConfigRepository.update(flowCommentConfigUpdateVo);
        if (updateRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.UPDATE_FAILED.getCode(), FlowCommonCodeConstants.UPDATE_FAILED.getDesc());
    }

    /**
     * 删除模板
     */
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody @Validated FlowCommentConfigDeleteVo flowCommentConfigDeleteVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        //是否需要验证该模板被引用的情况
        //todo
        Integer deleteRow = flowCommentConfigRepository.delete(flowCommentConfigDeleteVo);
        if (deleteRow > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(FlowCommonCodeConstants.DELETE_FAILED.getCode(), FlowCommonCodeConstants.DELETE_FAILED.getDesc());
    }

    /**
     * 模板详情
     */
    @PostMapping("/detail")
    public ResponseResult detail(@RequestBody @Validated FlowCommentConfigDetailVo flowCommentConfigDetailVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        ResponseResult detailResult = flowCommentConfigRepository.detail(flowCommentConfigDetailVo);
        return detailResult;
    }


}
